package nl.hr.minor.mobilelife.j1213.jlr.words;

import android.content.ClipData;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class LetterTile extends TextView implements OnDragListener, OnTouchListener {

	//private static final String LOG_TAG = LetterTile.class.getSimpleName();

	public static final int DROPZONE_INDEX_NO_POSITION = -1;

	/**
	 * Holds the position of the DropZone (from the DropZones list) this LetterTile is dropped on. 
	 * If the LetterTile is not on a DropZone, it has {link #DROPZONE_INDEX_NO_POSITION} as value.
	 */
	public int dropZoneIndex;
	public int startX, startY;

	public LetterTile(Context context) {
		super(context);
		dropZoneIndex = DROPZONE_INDEX_NO_POSITION;
		setBackgroundColor(Color.RED);
		setOnDragListener(this);
		setOnTouchListener(this);
	}

	// This is the method that the system calls when it dispatches a drag event to the
	// listener.
	public boolean onDrag(View v, DragEvent event) {

		if(v.equals(this))
			switch(event.getAction()) {
	
			case DragEvent.ACTION_DRAG_STARTED:
				Log.e("LetterTile","ACTION_DRAG_STARTED, letter: " + this.getTag());
				//this.setVisibility(View.GONE);
				return true;
	
			case DragEvent.ACTION_DRAG_ENTERED: 
			case DragEvent.ACTION_DRAG_LOCATION:
			case DragEvent.ACTION_DRAG_EXITED:
			case DragEvent.ACTION_DROP:
				return true;
			case DragEvent.ACTION_DRAG_ENDED:
				Log.e("LetterTile","ACTION_DRAG_ENDED, letter: " + this.getTag());
				//this.setVisibility(View.VISIBLE);
				return(true);
			default:
				Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
	
				return false;
			}
		else
			return false;
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v.equals(this)) {
			switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				Log.i("LevelActivity", "ACTION_DOWN, curDragItem == null, letter: " + v.getTag());
				startDrag(ClipData.newPlainText("", ""), new View.DragShadowBuilder(v), v, 0);
				return true;
			}
			return true;
		} else		
			return false;
	}
}
